<?php

class Emms_Form_Admin_PerformanceMetricOfficer extends HTech_Form
{
    protected $_primary  = 'PerformanceMetricCustomId';
    
    public function __construct() 
    {
        parent::__construct();
        
        $this->setName('PerformanceMetricOfficer');
        
        $this->setAttrib('title', $this->translate("Performance metric form"));
        $this->setDescription($this->translate("Edit performance metric info or add a new one"));
        
        $pmcId = new Zend_Form_Element_Hidden('PerformanceMetricCustomId');
        
        $officerUserId = new Zend_Form_Element_Select('OfficerUserId');
        $officerUserId->setLabel("Officer") 
            ->setRequired(true)
            ->addValidator('GreaterThan', true, array('min' => 0))
            ->addMultiOptions(Officers::getOfficerOptions())
            ->addErrorMessage($this->translate("Required field, can't be empty"));
        
        $performanceMetricId = new Zend_Form_Element_Select('PerformanceMetricId');
        $performanceMetricId->setLabel("Metric")
            ->setRequired(true)
            ->addValidator('NotEmpty', true, array(
                'messages' => $this->translate("Required field, can't be empty")))
            ->addValidator('GreaterThan', true, array(
                'min' => 0, 
                'messages' => $this->translate("Required field, can't be empty")))
            ->addValidator('Callback', true, array(
                'callback' => array($this, 'checkDuplicateCallback'), 
                'messages' => $this->translate("Non valid or duplicated custom metric")))
            ->addMultiOptions(PerformanceMetricsOfficers::getMetricOptions());
        
        $markA = new Zend_Form_Element_Text('A');
        $markA->setLabel("Mark A")
            ->setRequired(true)
            ->addValidator('NotEmpty', true, array('messages' => $this->translate("Required field, can't be empty")))
            ->addValidator('Regex', true, array(
                'pattern' => '(^\d+(\.\d{2})?$)',
                'messages' => $this->translate("Invalid mark")))
            ->addValidator('Callback', true, array(
                'callback' => array($this, 'checkMarkCallback'), 
                'messages' => $this->translate("Mark A must be lower than Mark B and Mark C")));
        
        $markB = new Zend_Form_Element_Text('B');
        $markB->setLabel("Mark B")
            ->setRequired(true)
            ->addValidator('NotEmpty', true, array('messages' => $this->translate("Required field, can't be empty")))
            ->addValidator('Regex', true, array(
                'pattern' => '(^\d+(\.\d{2})?$)',
                'messages' => $this->translate("Invalid mark")))
            ->addValidator('Callback', true, array(
                'callback' => array($this, 'checkMarkCallback'), 
                'messages' => $this->translate("Mark B must be greater than Mark A and lower than Mark C")));
        
        $markC = new Zend_Form_Element_Text('C');
        $markC->setLabel("Mark C")
            ->setRequired(true)
            ->addValidator('NotEmpty', true, array('messages' => $this->translate("Required field, can't be empty")))
            ->addValidator('Regex', true, array(
                'pattern' => '(^\d+(\.\d{2})?$)',
                'messages' => $this->translate("Invalid mark")))
            ->addValidator('Callback', true, array(
                'callback' => array($this, 'checkMarkCallback'), 
                'messages' => $this->translate("Mark C must be greater than Mark A and Mark B")));
        
        $submit = new Zend_Form_Element_Submit('Submit');
        $submit->setLabel("Submit")
            ->addDecorator(new HTech_Form_Decorator_Button(array('type'=>'submit')));
        
        $this->addElements(array($officerUserId,$performanceMetricId,$markA,$markB,$markC,$submit,$pmcId));
        
        if(!$this->_request->isPost()) {
            $param = $this->_request->getParams();
            if (isset($param['OfficerUserId'])) {
                $this->setDefaults(array('OfficerUserId' => (int) $param['OfficerUserId'])); 
            }
        }
        
    }
    
    public function checkDuplicateCallback($performanceMetricId, $args) 
    {
        $pmo = new PerformanceMetricsOfficers();
        $select = $pmo->select();         
        $select->where(sprintf("OfficerUserId = '%s'", $args['OfficerUserId']));
        $select->where(sprintf("PerformanceMetricId = '%s'", $performanceMetricId));
        $rows = $pmo->fetchAll($select)->toArray();
        if(count($rows) == 0) return true;
        if(count($rows) != 1) return false;
        if($rows[0]['PerformanceMetricCustomId'] == $args['PerformanceMetricCustomId']) return true;
        return false;
    }
    
    public function checkMarkCallback($mark, $args) 
    {
        if($args['A'] >= $args['B']) return false;
        if($args['B'] >= $args['C']) return false;
        return true;
    }
}